
#include "../includes.h"

//#define MAbs(x)	( ((x)>0) ? (x): -(x))

HEALTH_ HEALTH;

#define Proper_5V		5
//#define Proper_Vcc		12
int Proper_Vcc = 12;
#define Proper_Vee		-8

#define Allowable_Voltage_Error	0.1

#define ADC_SENSE_NULL 0
#define ADC_SENSE_5V 1
#define ADC_SENSE_8VP 2
#define ADC_SENSE_8VM 3

int adc_sensing_turn;

Diag_       DIAG, PrevDIAG;
OP_         PrevOperation;

char		STATUS_Warmup;

manual_diag_	manual_diag;

void Diagnose();
void Init_DIAG();


//char	once_receiver_detect;
int	last_init_eb;


void Init_DIAG(){

    PrevOperation.LOCKMODE = LOCKED;
    PrevOperation.OPMODE = UNDEFINED;
    PrevOperation.STATUS = UNDEFINED;

    DIAG.Watch1PPSCounter = 0;

	DIAG.DELETION_COUNTER = 0;

    DIAG.EB = OK;

    DIAG.EB_LOCK = FAIL;
    DIAG.OCXO = OK;
    DIAG.POWER = OK;
    DIAG.power_fault_counter = 0;
    DIAG.power_good_counter = 0;
    DIAG.EFC = OK;
    DIAG.ESYNC = OK;
	DIAG.dDAC = OK;
	DIAG.PLL = NO;
	DIAG.COUNTER = NO;
	DIAG.OSC = YES;
	DIAG.RTC = OK;
	DIAG.EJT = FAIL;

	// 1pps alarm
	DIAG.PPS = OK;
	DIAG.PPS_ALARM_FLAG = OK;
	DIAG.PPS_FAULT_FLAG = OK;
	DIAG.PPS_WatchCounter = 0;
	DIAG.PPS_SWITCH_COUNTER = 0;
	DIAG.PPS_RECOVERY_FLAG = OFF;

	DIAG.TEMPERATURE = OK;

	DIAG.SYSTEM = OK;

	STATUS_Warmup = ON;

    DIAG._10MHz = OK;
    DIAG._FAULT = OFF;
    DIAG.PP2S_OFFSET = OK;

	DIAG._10MHz_GoodCounter = 100;

	DIAG.RB = OK;

	manual_diag.rtc = OK;
	manual_diag.osc = OK;
	manual_diag.power = OK;


	SYSTEM.EB.once_receiver_detect = OFF;
	
	DIAG.TFOM = 9;
	last_init_eb = -(30*60);
	
	DIAG.DUAL_RX = UNDEFINED;
	DIAG.DUAL_TX = UNDEFINED;
	DIAG.DUAL_RX_CRC = UNDEFINED;
	DIAG.DUAL_EXIST = UNDEFINED;
	DIAG.DUAL_TEST = UNDEFINED;
	DIAG.TRACE_10M_COUNTER = UNDEFINED;
	
	DIAG.DualRxFailCounter=0;
	DIAG.DualRxFailHopeless=0;
	DIAG.DualRxChkOKCounter=0;
	
	adc_sensing_turn = ADC_SENSE_NULL;
	
	DIAG.me_exist_ok_counter = 0;
	DIAG.me_exist_nok_counter = 0;
	DIAG.you_exist_ok_counter = 0;
	DIAG.you_exist_nok_counter = 0;
	
	DIAG.i_exist = NO;
	DIAG.u_exist = NO;
	
	DIAG.DUAL_FAIL_LED=0;
}


void Diagnose()
{
	int t1;
	float	pps_fault_range;
	int offset;
	float volt_temp;

	if( FIRST_LOCK ){
		MonitorVCXOEfc();
	}


	//-----------
	// DIAG.RTC
	//-----------

	/*
	if(rtc.start){
		t1 = check_rtc();

		if(	t1 == FAIL ){
			DIAG.RTC = FAIL;
   	    	if (PrevDIAG.RTC == OK ) {
       			LogIt(LOG_RTC_Error,0);
       		}
		}
		else if( t1 == OK ) {
			DIAG.RTC = OK;
		}
	}
	else {
		start_rtc();
		if(GC.RunningTime > 60){
			DIAG.RTC = FAIL;
   	    	if (PrevDIAG.RTC == OK ) {
       			LogIt(LOG_RTC_Error,1);
       		}
		}
	}
	*/
	DIAG.RTC = OK;
	
	if(DIAG.i_exist == NO){
		DIAG.EjtOKCounter = 0;
		
		if(PrevDIAG.EJT == OK){
			DIAG.EJT = FAIL;
			LogItString(LOG_CATA1_NORM, LOG_CATA2_MINUS_ME, "Ejector open");
			
			backup_copy_log();
			
			LOG.CNT = 0;
			LOG.TimeOut = 0;
						
			//dual_test_start = YES;
			//dual_test_retry = 0;
			//DIAG.DUAL_TEST = UNDEFINED;
			//DIAG.TRACE_10M_COUNTER = UNDEFINED;
			SetDualTxModeToOperation();
		}
		
		//DIAG.DUAL_RX = UNDEFINED;
		//DIAG.DUAL_RX_CRC = UNDEFINED;
		//dual_test_start = YES;
		//dual_test_retry = 0;
		//DIAG.DUAL_TEST = UNDEFINED;
		//DIAG.TRACE_10M_COUNTER = UNDEFINED;
	}
	else{
		DIAG.EjtOKCounter++;
		
		//if(DIAG.EjtOKCounter>10){
			if(PrevDIAG.EJT == FAIL){
				LogItString(LOG_CATA1_NORM, LOG_CATA2_PLUS_ME, "Ejector ok");
				
				backup_copy_log();
				
				LOG.CNT = 0;
				LOG.TimeOut = 0;
				
				dual_test_start = YES;
				dual_test_retry = 0;
				DIAG.DUAL_TEST = UNDEFINED;
				DIAG.TRACE_10M_COUNTER = UNDEFINED;
			}
			DIAG.EJT = OK;
			
		//}
	}


	//-----------
	// DIAG.EB
	//-----------
    // EB serial

	if(SYSTEM.EB.once_receiver_detect){
//   		if (SYSTEM.EB.STATUS == EB_STATUS_NONE) {
   		//if (SYSTEM.EB.RxCheckCounter > 60)
   		//{
   		//	if(waiting_change_eb) {
		//		if(waiting_change_eb>MAX_EB_RESET) {DIAG.EB = FAIL;}
		//	}
		//    else{
		//    	DIAG.EB = FAIL;
		//    }
		if ( (1||CONFIG.RcvType==1) && SYSTEM.EB.RxCheckCounter > 60 &&
			(waiting_change_eb == 0 || waiting_change_eb > MAX_EB_RESET) &&
    		(skipping_change_eb == 0)) 
    	{
	   		DIAG.EB = FAIL;
	
		    if(DIAG.EB == FAIL && PrevDIAG.EB == OK){
		    	if(SYSTEM.EB.STATUS == EB_STATUS_RUN){
		    		LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
		    			LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_INITSTEP, 2);
		    	}

		    	MessageOut((BYTE*)"GPS Receiver - out\r\n");

		    	Init_EB(INIT_EB_SILENT, INIT_EB_SECOND, NO);
		    	EB_Task_start();
		    	LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_EB_Error,0);
		    }
   		}
   		else {
   			DIAG.EB = OK;
   		}
   	}
   	else {
   		DIAG.EB = OK;
   	}

	if(fail_eb_now == YES) {DIAG.EB = FAIL;}

    /* 1pps watchdog -> this counter cleared at 1PPS interrupt */
    //=================================================================
    // Locked 1PPS  OCXO


    if((DIAG.Watch1PPSCounter++ > 10) && (startup.SELFTEST == OK)) {

        DIAG.OCXO = FAIL;
		//TLogIt(103,1);
    }
    if(DIAG.Watch1PPSCounter == 0){
       	DIAG.OCXO = OK;
   	}

	//---------------------------------------------------------------
	// DIAG.EFC - efc at near of full range ( LED ALARM : OPERATION )
	//---------------------------------------------------------------
    if( SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION || frc_error.efc) {

		//if(((SYSTEM.OPERATION.STATUS == STATUS_ACTIVE) || (SYSTEM.OPERATION.STATUS == STATUS_STANDBY))){
		if(FIRST_LOCK || frc_error.efc){
	    	if( OSC.kind == OCXO_OSC) {
		    	if( (CONTROL.RealDAC ) > EFC_UPPER_LIMIT || (CONTROL.RealDAC) < EFC_LOWER_LIMIT || frc_error.efc ){
		    		SetOcxoEfc();
    		    	DIAG.EFC = FAIL;

    		    	if (PrevDIAG.EFC == OK ) {
        				//LogIt(LOG_EFC_ERROR,0);
		        		log_efc_err();
		        		if( (1||CONFIG.RcvType==1)){
							LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING,
								LOG_SYNC15_RESET, RESET_MODEL_BOTH, RESET_TYPE_INITSTEP, 3);
							Init_EB(INIT_EB_SILENT, INIT_EB_SECOND, YES);
							EB_Task_start();
		        		}
        			}
		        } else {
		        	ClrOcxoEfc();
    		    	DIAG.EFC = OK;
    				if (PrevDIAG.EFC == FAIL ) {
			       		//log_efc_ok();
			       	}
		       	}
        	}
        	else {
		    	if( (CONTROL.RealDAC) > EFC_RB_UPPER_LIMIT || (CONTROL.RealDAC) < EFC_RB_LOWER_LIMIT || frc_error.efc ){
		    		SetOcxoEfc();
    		    	DIAG.EFC = FAIL;

    		    	if (PrevDIAG.EFC == OK ) {
        				//LogIt(LOG_EFC_ERROR,1);
        				//log_efc_err();
        			}
		        } else {
		        	ClrOcxoEfc();
    		    	DIAG.EFC = OK;
        		}
        	}
        }
    } else {
        DIAG.EFC = OK;
        ClrOcxoEfc();
    }


	//t1 = abs((int)(CONTROL.AvgTimeOffset*BILLION));

	//if( (FIRST_LOCK) && !(TOD.status & BIT_STATUS_HOLD_OVER)) {
    //
	//	if ( t1 < 10 ) {			DIAG.TFOM = 2;	}
	//	else if ( t1 < 100 ) {		DIAG.TFOM = 3;	}
	//	else if ( t1 < 1000 ) {		DIAG.TFOM = 4;	}
	//	else if ( t1 < 10000 ) {	DIAG.TFOM = 5;	}
	//	else if ( t1 < 100000 ) {	DIAG.TFOM = 6;	}
	//	else if ( t1 < 1000000 ) {	DIAG.TFOM = 7;	}
	//	else if ( t1 < 10000000 ) {	DIAG.TFOM = 8;	}
	//	else {						DIAG.TFOM = 9;	}
	//}
	//else {
	//	DIAG.TFOM = 9;
	//}

	offset = (int)(CONTROL.AvgTimeOffset*TENMILLION);
	
	if(	(FIRST_LOCK) && (SYSTEM.OPERATION.LOCKMODE == LOCKED) && (STATUS_Warmup == OFF) &&
		(RECOVERY.flag == OFF) && (CONTROL.StepCounter >= 2)) {
		//(SYSTEM.EB.LOCKMODE == EB_UNLOCKED) && (RECOVERY.flag == OFF) && (CONTROL.StepCounter >= 2)) {
		//offset = (int)(CONTROL.AvgTimeOffset*TENMILLION);
		
		//if((offset > 10) || (offset < -10) && KInterval==Kinterval_STEP_1){CONTROL.StepCounter = 10;}
		if( ((offset > 20) || (offset < -20)) && KInterval==Kinterval_STEP_1 ){CONTROL.StepCounter = 1;}
		if((MeanDAC-CONTROL.RealDAC>200.0 || MeanDAC-CONTROL.RealDAC<-200.0) && KInterval==Kinterval_STEP_1){
			if(!((VME.Data.TimeSync_Mode==VME_CV_MODE)&&(CONFIG.iCVType == 1)))CONTROL.StepCounter = 1;
		}

		//if( (offset > 10) || (offset < -10) ) {
		if( (offset > 100) || (offset < -100) ) {

			//if( KInterval != Kinterval_STEP_1 && (SYSTEM.OPERATION.CTRLMODE == CTRL_LOCKEDtoGPS) ){
			if( PrevDIAG.PPS == OK && KInterval != Kinterval_STEP_1 && GC.RunningTime-last_init_eb > 30*60 && (1||CONFIG.RcvType==1)){
					LogIt3(LOG_CATA1_NORM, LOG_CATA2_WARN, 
						LOG_SYNC15_RESET, RESET_MODEL_BOTH, RESET_TYPE_INITSTEP, 12);
					Init_EB(INIT_EB_SILENT, INIT_EB_SECOND, YES);
					EB_Task_start();
					bResetReceiver = 1;
			}
			if( KInterval != Kinterval_STEP_1) {ChangeControlInterval(Kinterval_STEP_1);}
		}

		if( (offset > CONFIG.AlarmRange) || (offset < -CONFIG.AlarmRange) ) {
			DIAG.PPS_ALARM_FLAG = FAIL;

		} else {
			DIAG.PPS_ALARM_FLAG = OK;
		}

		pps_fault_range = CONFIG.AlarmRange*1.5;

		if( (offset > pps_fault_range) || (offset < -pps_fault_range) ) {
			DIAG.PPS_FAULT_FLAG = FAIL;
		} else {
			DIAG.PPS_FAULT_FLAG = OK;
		}
	}
	//else {
	//	DIAG.PPS_ALARM_FLAG = DIAG.PPS_FAULT_FLAG = OK;
	//}

	if((SYSTEM.OPERATION.STATUS == STATUS_ACTIVE) &&
		(offset > 10 || offset <-10))
	{
		over_1us = YES;
	}
	else{
		over_1us = NO;
	}
		
		
	if( SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION){
		if( DIAG.PPS_ALARM_FLAG == FAIL) {
			DIAG.PPS = FAIL;

			if ( PrevDIAG.PPS == OK ) {
				log_1pps_alarm_on();
			}
		}
		else {
			DIAG.PPS = OK;
			
			if ( PrevDIAG.PPS == FAIL ) {
				log_1pps_alarm_off();
			}
		}
	}

	if( DIAG.PPS == FAIL ) {
		if(DIAG.PPS_SWITCH_COUNTER < 20 ) DIAG.PPS_SWITCH_COUNTER++;
	} else {
		if(DIAG.PPS_SWITCH_COUNTER > 0 ) DIAG.PPS_SWITCH_COUNTER--;
	}


	//-----------
	// DIAG.POWER
	//-----------
	//kang MonitorPlus5V();
	//kang MonitorPlusVcc();
	//kang MonitorMinusVee();
	
	if((adc_sensing_turn == ADC_SENSE_5V) || (adc_sensing_turn == ADC_SENSE_NULL)){
		SelectP5VtoSense(fdFPGA);
		ps.p5v = PLUS_5V_MULTIPLY_VALUE * VREF * ((float) ioctl(fdFPGA, IOCTL_ADC_GET_VAL, 0) / (float) ADC_MAX_VALUE);
		fPlus5V = ps.p5v;
	}
	
	if((adc_sensing_turn == ADC_SENSE_8VP) || (adc_sensing_turn == ADC_SENSE_NULL)){
		SelectP8VtoSense(fdFPGA);
		ps.p8v = VCC_MULTIPLY_VALUE * VREF * ((float) ioctl(fdFPGA, IOCTL_ADC_GET_VAL, 0) / (float) ADC_MAX_VALUE);
		fPlusVcc = ps.p8v;
	}

	if((adc_sensing_turn == ADC_SENSE_8VM) || (adc_sensing_turn == ADC_SENSE_NULL)){
		if(CONFIG.HWrevision == 1){
			SelectN8VtoSense(fdFPGA);
			ps.n8v = VEE_MULTIPLY_VALUE * VREF * ((float) ioctl(fdFPGA, IOCTL_ADC_GET_VAL, 0) / (float) ADC_MAX_VALUE);
		}
		fMinusVee = ps.n8v;
	}
	
	DeSelectSense(fdFPGA);

	if(adc_sensing_turn == ADC_SENSE_NULL){adc_sensing_turn = ADC_SENSE_5V;}
	else if(adc_sensing_turn == ADC_SENSE_5V){adc_sensing_turn = ADC_SENSE_8VP;}
	else if(adc_sensing_turn == ADC_SENSE_8VP){adc_sensing_turn = ADC_SENSE_8VM;}
	else{adc_sensing_turn = ADC_SENSE_5V;}
	//fPlus5V = ps.p5v;
	//fPlusVcc = ps.p8v;
	//fMinusVee = ps.n8v;
	

	if(CONFIG.HWrevision == 1){
		Proper_Vcc = 8;
	}
	else{
		Proper_Vcc = 12;
		fMinusVee=Proper_Vee;
	}
/*
	volt_temp = fPlusVcc-(float)Proper_Vcc;
	if(volt_temp<0) volt_temp = -volt_temp;
		
	//if( MAbs(fPlusVcc-Proper_Vcc) <=	(Proper_Vcc*Allowable_Voltage_Error + 0.5) ) {
	if( volt_temp <= ((float)Proper_Vcc*(float)Allowable_Voltage_Error + 0.5)) {
		DIAG.power_fault_counter = 0;
	}
	else {
		DIAG.power_fault_counter++;
	}
*/

//printf("<5v:%.5f, Vcc:%.5f, Vee:%.5f>",fPlus5V,fPlusVcc,fMinusVee);
	
	if( (fPlus5V < Proper_5V - (Proper_5V*Allowable_Voltage_Error)) || 
		(fPlus5V > Proper_5V + (Proper_5V*Allowable_Voltage_Error)) ||
		(fPlusVcc < Proper_Vcc - (Proper_Vcc*Allowable_Voltage_Error)) ||
		(fPlusVcc > Proper_Vcc + (Proper_Vcc*Allowable_Voltage_Error)) ||
		(fMinusVee < Proper_Vee + (Proper_Vee*Allowable_Voltage_Error)) ||
		(fMinusVee > Proper_Vee - (Proper_Vee*Allowable_Voltage_Error)) 
		//||(DIAG.DELETION_COUNTER>0) 
	)
	{
		DIAG.power_fault_counter++;
		DIAG.power_good_counter = 0;
	}
	else{
		DIAG.power_good_counter++;
		DIAG.power_fault_counter = 0;
	}
	
//--------------------------------------	
	//DIAG.power_fault_counter = 0;
//--------------------------------------
	
	if(DIAG.power_fault_counter >= 10){
		DIAG.power_fault_counter = 20;
		
		DIAG.POWER = FAIL;
		if( PrevDIAG.POWER == OK ){
			//LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_POWER_FAIL,0);
			LogIt3(LOG_CATA1_NORM, LOG_CATA2_FAIL, 
				LOG_POWER_FAIL, (int)(fPlus5V*100), (int)(fPlusVcc*100), (int)(fMinusVee*100)
			);
		}
	}
	else {
		if(DIAG.power_good_counter >= 10){
			DIAG.power_good_counter = 20;
			
			DIAG.POWER = OK;
			if( PrevDIAG.POWER == FAIL ){
				//LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_POWER_OK,0);
				LogIt3(LOG_CATA1_NORM, LOG_CATA2_OK, 
					LOG_POWER_OK, (int)(fPlus5V*100), (int)(fPlusVcc*100), (int)(fMinusVee*100)
				);
			}
		}
	}

    if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG3){
		sprintf((char*)MESG,"5V:%.1f Vcc:%.1f Vee:%.1f\r\n", fPlus5V, fPlusVcc, fMinusVee);
		TransmitMessage((BYTE*)MESG,DEBUG_PORT);
	}


	if(TRACE.EVENT == OFF){
//	    if(startup.SELFTEST == OK) {
			if( 0 ){
			    if(GC.RunningTime > 10 && startup.SELFTEST == OK) {
					DIAG._10MHz_GoodCounter = 0;
				}
			    ClearFail();
	    	}
	    	else {
				if(DIAG._10MHz_GoodCounter < 100){
					DIAG._10MHz_GoodCounter++;
				}
	    	}
//	    }
//	    else {

//	    }
	}



 	if(DIAG._10MHz_GoodCounter == 0 || frc_error.ocxo  || b10msIntError1st ){

		DIAG._10MHz = FAIL;
		SetOcxoFault();

		if ( PrevDIAG._10MHz == OK ) {
			//LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_10MHz,0);
		}
	}
	else if(DIAG._10MHz_GoodCounter > 10){
		ClrOcxoFault();
		DIAG._10MHz = OK;
	}


	//----------------------
	// DIAG.RB
	//----------------------
 	dbgCV(150) printf("%d, %d, %d, %d\n",CGM_P(),GC.RunningTime,!IsSRO100Lock(),rb.tx_error);
//	if( (CGM_P() && GC.RunningTime > 10*60 &&  !IsX72Lock()) || (rb.tx_error >= 5) ){
	if( (CGM_P() && GC.RunningTime > 10*60 &&  !IsSRO100Lock()) || (rb.tx_error >= 5) ){
		DIAG.RB = FAIL;
		if ( PrevDIAG.RB == OK ) {
			if(rb.tx_error >= 5){
				LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_RB_TX_Error,0);
			}
			else{
				LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_RB_LOCK_Error,0);
			}
		}
	}
	else {
		DIAG.RB = OK;
	}


	//----------------------
	// DIAG.OSC
	//----------------------
	if(DIAG._10MHz == FAIL || DIAG.OCXO == FAIL || DIAG.EFC == FAIL || DIAG.RB == FAIL ){
		DIAG.OSC = FAIL;
		if ( PrevDIAG.OSC == OK ) {
			//LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_OCXO_Error,0);
		}
	}
	else {
		DIAG.OSC = OK;
	}


	//----------------------
	// DIAG.TEMPERATURE
	//----------------------
	if( TEMPERATURE.AVERAGE >= 90 || TEMPERATURE.AVERAGE <= -10 ) {
		DIAG.TEMPERATURE = FAIL;
	} else {
		DIAG.TEMPERATURE = OK;
	}


	if( DIAG._10MHz == FAIL ){
    	DIAG._FAULT = ON;
    }
    else {
    	DIAG._FAULT = OFF;
    }

/*
	if( startup.SELFTEST ) {
//		if( DIAG._10MHz != FAIL && DIAG.POWER != FAIL &&   DIAG.EB != FAIL 	){
		if( DIAG.OSC != FAIL && DIAG.POWER != FAIL &&   DIAG.EB != FAIL 	){

			SYSTEM_FAIL_OFF();
    	}
    	else {
    		SYSTEM_FAIL_ON();
    	}
    }
    else {
		SYSTEM_FAIL_OFF();
    }
*/


	if(manual_diag.rtc == FAIL) {		DIAG.RTC = FAIL;	}
	if(manual_diag.osc == FAIL) {		DIAG.OSC = FAIL;	}
	if(manual_diag.power == FAIL) {		DIAG.POWER = FAIL;	}

	if(SYSTEM.OPERATION.STATUS == STATUS_ACTIVE){DIAG.TRACE_10M_COUNTER = UNDEFINED;}
	//kang 
	// && DIAG.RTC && DIAG.EB && DIAG.ESYNC){
	//hmlee - even if it's active, DIAG.DUAL_RX,DIAG.DUAL_TEST,DIAG.TRACE_10M_COUNTER will be UNDEFINED
	//if(DIAG.POWER && DIAG.OSC && DIAG.DUAL_RX && DIAG.DUAL_TEST && DIAG.TRACE_10M_COUNTER)
	//if(DIAG.POWER && DIAG.OSC && DIAG.DUAL_TEST && DIAG.TRACE_10M_COUNTER && HEALTH.EB)

	DIAG.DUAL_TEST = OK;
	DIAG.TRACE_10M_COUNTER = OK;
	DIAG.POWER = OK;

	if(	DIAG.dDAC == OK && DIAG._FAULT == OFF ) {
		DIAG.SYSTEM = OK;
	}
	else {
		DIAG.SYSTEM = FAIL;
	}

	if( DIAG.POWER && DIAG.OSC && DIAG.DUAL_TEST && DIAG.TRACE_10M_COUNTER )
	{
		if((HEALTH.EB == FAIL) && (SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION)){
			if((PrevDIAG.SYSTEM != FAIL) && (no_system_fail==0)){
					sprintf(MESG,"System, pwr:%d, osc:%d, drx:%d, dch:%d, t10:%d, eb:%d",
						DIAG.POWER, DIAG.OSC, DIAG.DUAL_RX,
						DIAG.DUAL_TEST, DIAG.TRACE_10M_COUNTER, HEALTH.EB
					);
					LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
			}
		
			if(no_system_fail){
				DIAG.SYSTEM = OK;
			}
			else{
				DIAG.SYSTEM = FAIL;
			}
		}
		else{
			if(PrevDIAG.SYSTEM != OK){
				sprintf(MESG,"System, pwr:%d, osc:%d, drx:%d, dch:%d, t10:%d, eb:%d",
					DIAG.POWER, DIAG.OSC, DIAG.DUAL_RX,
					DIAG.DUAL_TEST, DIAG.TRACE_10M_COUNTER, HEALTH.EB
				);
				LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, MESG);
			}
			DIAG.SYSTEM = OK;
		}
	}
	else {
		if((PrevDIAG.SYSTEM != FAIL) && (no_system_fail==0)){
			sprintf(MESG,"System, pwr:%d, osc:%d, drx:%d, dch:%d, t10:%d, eb:%d",
				DIAG.POWER, DIAG.OSC, DIAG.DUAL_RX,
				DIAG.DUAL_TEST, DIAG.TRACE_10M_COUNTER, HEALTH.EB
			);
			LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
		}
		
		if(no_system_fail){DIAG.SYSTEM = OK;}
		else{DIAG.SYSTEM = FAIL;}
		
		//kang printf("POWER:%d, OSC:%d, RTC:%d, EB:%d, ESYNC:%d\n", DIAG.POWER, DIAG.OSC, DIAG.RTC, DIAG.EB, DIAG.ESYNC);
	}


 	PrevDIAG = DIAG;

/*
	if(LOCK_REFERENCE){
	    if (PrevOperation.LOCKMODE != SYSTEM.OPERATION.LOCKMODE) {
	        if ( SYSTEM.OPERATION.OPMODE != OPMODE_POWERUP){
	        	if( SYSTEM.OPERATION.LOCKMODE == LOCKED ) {
	        		LogIt(LOG_Lock,0);
	        	}
	        	else if (SYSTEM.OPERATION.LOCKMODE != LOCKED ) {
	        		LogIt(LOG_Holdover,0);
	        	}
	        }
	    }
	}
*/

    PrevOperation = SYSTEM.OPERATION;

	//if((DIAG.EB==FAIL) || (SYSTEM.EB.STATUS != EB_STATUS_RUN) || (DIAG.EB_LOCK==FAIL)){
	if((DIAG.EB==FAIL) || (DIAG.EB_LOCK==FAIL)){
		if(waiting_change_eb) {
			if(waiting_change_eb>MAX_EB_RESET) {HEALTH.EB = FAIL;}
		}
		else if(skipping_change_eb){}
		else{
			HEALTH.EB = FAIL;
		}
	}
	else {
		HEALTH.EB = OK;
	}

	if(fail_eb_now == YES) {HEALTH.EB = FAIL;}
		
		
	switch( SYSTEM.OPERATION.STATUS ){
		case STATUS_ACTIVE:				PROMPT[6] = 'A';				break;
		case STATUS_BLOCK:				PROMPT[6] = 'B';				break;
		//case STATUS_PRE_STANDBY:		PROMPT[6] = 'B';				break;
		case STATUS_STANDBY:			PROMPT[6] = 'S';				break;
	}

	if(USER.MANUAL_CONTROL == ON){
		PROMPT[6] = 'M';
	}



	if(DIAG.POWER){
		ResetPwrFail();
	}
	else {
		SetPwrFail();
	}

/*
	if(startup.SELFTEST == OK) {

		//if(DIAG.POWER && DIAG.OSC && DIAG.RTC && !(LOCK_REFERENCE&&((SYSTEM.OPERATION.LOCKMODE == HOLDOVER_OVER10MIN)))){
		// [2003-01-02 2:31�� Reference fail�?larm ��f�?!
		if(DIAG.SYSTEM){
			ResetFuncFail();
			led_board_fail_flag = OFF;
		}
		else {
			SetFuncFail();
			led_board_fail_flag = ON;
		}
	}
	else {
		SetFuncFail();
		led_board_fail_flag = ON;
	}
*/

	if(CONFIG.AntCheckFlag){

        if(startup.SELFTEST == OK){
			//if(((SYSTEM.EB.LOCKMODE == EB_UNLOCKED) && (CHECKLOCK.UnlockedCounter > 600))  ||
			if(((SYSTEM.EB.LOCKMODE == EB_UNLOCKED) && (CHECKLOCK.UnlockedCounter > 60*60))  ||
				(DIAG.AntennaFaultCounter > 60 )){
				if(waiting_change_eb) {
					if(waiting_change_eb>MAX_EB_RESET) {DIAG.EB_LOCK = FAIL;}
				}
		    	else{
		    		DIAG.EB_LOCK = FAIL;
		    	}
		    	
		    	if(DIAG.EB_LOCK == FAIL && PrevDIAG.EB_LOCK == OK){
		    		LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_EB_Unlock,0);
		    		LogEbStatus();
		    	}
			}
			else if((SYSTEM.EB.LOCKMODE != EB_UNLOCKED) && (CHECKLOCK.LockedCounter >= 20)) {
				DIAG.EB_LOCK = OK;
				if(PrevDIAG.EB_LOCK != OK){
					LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_EB_Lock,0);
				}
			}
		}
        else {
			if((SYSTEM.EB.LOCKMODE != EB_UNLOCKED) && (CHECKLOCK.LockedCounter >= 20)) {
		    	DIAG.EB_LOCK = OK;
		    }
		    else if(skipping_change_eb){}
		    else {
		    	if(waiting_change_eb) {
					if(waiting_change_eb>MAX_EB_RESET) {DIAG.EB_LOCK = FAIL;}
				}
		    	else{
		    		DIAG.EB_LOCK = FAIL;
		    	}
		    }
        }
	}
	else {

        if(startup.SELFTEST == OK){
			//if((SYSTEM.EB.LOCKMODE == EB_UNLOCKED) && (CHECKLOCK.UnlockedCounter > 600)){
			if((SYSTEM.EB.LOCKMODE == EB_UNLOCKED) && (CHECKLOCK.UnlockedCounter > 60*60)){
				if(waiting_change_eb) {
					if(waiting_change_eb>MAX_EB_RESET) {DIAG.EB_LOCK = FAIL;}
				}
		    	else{
		    		DIAG.EB_LOCK = FAIL;
		    	}
		    	
		    	if(DIAG.EB_LOCK == FAIL && PrevDIAG.EB_LOCK == OK){
		    		LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_EB_Unlock,0);
		    		LogEbStatus();
		    	}
			}
			else if((SYSTEM.EB.LOCKMODE != EB_UNLOCKED) && (CHECKLOCK.LockedCounter >= 20)) {
				DIAG.EB_LOCK = OK;
				if(PrevDIAG.EB_LOCK != OK){
					LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_EB_Lock,0);
				}
			}
		}
        else {
			if((SYSTEM.EB.LOCKMODE != EB_UNLOCKED) && (CHECKLOCK.LockedCounter >= 20)) {
		    	DIAG.EB_LOCK = OK;
		    }
		    else {
				if(waiting_change_eb) {
					if(waiting_change_eb>MAX_EB_RESET) {DIAG.EB_LOCK = FAIL;}
				}
				else if(skipping_change_eb){}
		    	else{
		    		DIAG.EB_LOCK = FAIL;
		    	}
		    }
        }
	}


/*
					sprintf(MESG,"%3d [%d %d %d %d %d %d %d %d]",
						DIAG._10MHz_GoodCounter,
						DIAG.Watch1PPSCounter,
						DIAG.OSC,
						DIAG.OCXO,
						DIAG.OCXO,
						DIAG.POWER,
						DIAG._10MHz,
						DIAG._FAULT,
						DIAG.EB					);
					MessageOut(MESG);
*/

 	if(Status_DualRx == NO){
 		if(PrevStatus_DualRx != Status_DualRx){
 			if ( SYSTEM.OPERATION.OPMODE != OPMODE_POWERUP){
 				LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_DUAL_DATA_NOT_RECEIVE,Status_DualRx);
 			}
 		}
 	}

 	PrevStatus_DualRx = Status_DualRx;
}


